package com.example.test_demo.test;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @program: Demo
 * @description: 文件工具
 * @author: yanghua
 * @create: 2021-06-25 17:39
 **/
public class fileutil {
    public static void main(String[] args) throws IOException {
        getFilename();
        //zsdtus00020
    }
    public static void getFilename() throws IOException {
        var s ="ss";
        var path="D:\\汽车部件数据训练\\Annotations";
        var newpath="D:\\汽车部件数据训练\\txt";
        File file = new File(path);
        if (!file.exists()){
            return;
        }
        String content = null;
        FileWriter fw = null;
        File[] files = file.listFiles();
        int cnt=0;
        for (int i = 0; i <files.length ; i++) {
            cnt++;
            try
            {
            File f = files[i];
            //2020-01-11_21_41_15_002   2020-03-07_07_12_50_138  2021-05-17-13-16-42
            System.out.println(f.getName());
            if (f.getName().contains("2020-01")){

                content=f.getName().split("\\.")[0]+".jpg, 0";
            }
            if (f.getName().contains("2020-03")){
                content=f.getName().split("\\.")[0]+".jpg, 1";
            }
            if (f.getName().contains("2020-05-15")||f.getName().contains("2020-05-16")||f.getName().contains("2020-05-17")){
                content=f.getName().split("\\.")[0]+".jpg, 2";
            }
            if (f.getName().contains("2020-05-21")||f.getName().contains("2020-05-22")||f.getName().contains("2020-05-25")||f.getName().contains("2020-05-27")||f.getName().contains("2020-05-29")){
                content=f.getName().split("\\.")[0]+".jpg, 3";
            }
            //创建txt文件
            File file1 = new File(newpath +"\\"+ f.getName().split("\\.")[0] + ".txt");
            if (!file1.exists()){
                file1.createNewFile();
            }
            fw = new FileWriter(newpath +"\\"+ f.getName().split("\\.")[0] + ".txt");
            fw.write(content);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            finally
            {
                try
                {
                    fw.close();
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }

            }

        }
        System.out.println(cnt);

    }

}
